package aom.ju.ss.view.dialog

import android.app.Activity
import android.text.TextUtils
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.DialogCommentBinding
import aom.ju.ss.databinding.ItemCommentBinding
import aom.ju.ss.lpInterface.BindViewInterface
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.UserInfo.Companion.userInfo
import aom.ju.ss.mode.VideoComment
import aom.ju.ss.mode.VideoCommentData
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.TimeUtil
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.LP_API.GET_LIVE_COMMENT
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.PAdapter
import com.bumptech.glide.Glide
import com.google.gson.Gson
import java.util.*

class CommentDialog(
    var ac: Activity,
    val id: String,
    var commentCallback: CommentCallback
) : BaseDialog(ac) {

    override fun getLay(): Int {
        return R.layout.dialog_comment
    }

    init {
        setWidth(-1)
    }

    override fun isBottom(): Boolean {
        return true
    }

    private lateinit var adapter: PAdapter
    private lateinit var binding: DialogCommentBinding


    override fun init() {
        binding = vBinding as DialogCommentBinding
        binding.ivClose.setOnClickListener { dis() }
        binding.rec.layoutManager = LinearLayoutManager(ac)

        val dataList: MutableList<VideoCommentData> = arrayListOf()

        adapter = PAdapter(
            dataList, R.layout.item_comment
        ) { bbc, position ->
            val b = bbc as ItemCommentBinding
            b.tvName.text = dataList[position].nickname
            b.tvTime.text = dataList[position].time
            Glide.with(ac).load(dataList[position].headimgurl)
                .into(b.ivUserHead)
            b.tvCommentContent.text = dataList[position].content
        }
        binding.rec.adapter = adapter
        binding.tvVideoSendComment.setOnClickListener {
            val comment = binding.etVideoComment.text.toString()
            if (TextUtils.isEmpty(comment)) {
                "请输入评论内容".show
                return@setOnClickListener
            }

            OKHttpUtils(activity, false)
                .SetApiUrl(LP_API.SEND_LIVE_COMMENT)
                .SetKey("id", "content")
                .SetValue(id, comment)
                .POST(object : MyHttpCallBack {

                    override fun ok(jsonString: String?, httpTY: String?) {
                        commentCallback.commentCallback()
                        "评论成功!".show
                        binding.etVideoComment.setText("")

                        val commentEntity = VideoCommentData(
                            comment,
                            userInfo!!.headimgurl,
                            userInfo!!.member_id,
                            userInfo!!.nickname,
                            TimeUtil.getYearMonthAndDayWithHourmmss(System.currentTimeMillis())
                        )
                        dataList.add(commentEntity)
                        adapter.notifyDataSetChanged()
                    }

                    override fun error(
                        e: String?,
                        jsonString: String?,
                        httpTY: String?
                    ) {
                        e?.show
                    }
                })

            binding.title.text = "共" + dataList.size + "条评论";
        }
        binding.small.setOnLoadMoreListener {
            page++
            getData(dataList)
        }
        binding.small.setOnRefreshListener {
            page = 1
            dataList.clear()
            getData(dataList)
        }
        binding.small.autoRefresh()

    }


    interface CommentCallback {
        fun commentCallback()
    }

    private lateinit var mode: VideoComment
    private var page = 1
    private fun getData(dataList: MutableList<VideoCommentData>) {
        OKHttpUtils(ac, false)
            .SetApiUrl(GET_LIVE_COMMENT)
            .SetKey("id", "page")
            .SetValue(
                id, page.toString() + ""
            ).POST(object : MyHttpCallBack {
                override fun ok(jsonString: String, httpTY: String) {
                    binding.small.finishLoadMore()
                    binding.small.finishRefresh()
                    mode = Gson().fromJson(jsonString, VideoComment::class.java)
                    mode.data?.let {
                        binding.title.text = "共" + it.size + "条评论"
                        dataList.addAll(it)
                        LogUtils.e(Gson().toJson(it))

                    }
                    adapter.notifyDataSetChanged()
                }

                override fun error(e: String, jsonString: String, httpTY: String) {
                    binding.small.finishLoadMore()
                    binding.small.finishRefresh()
                }
            })
    }
}